﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BasicClass;
using Common;
using DealLog;

namespace DealPLC
{
    /// <summary>
    /// TypePLC.xaml 的交互逻辑
    /// </summary>
    public partial class UCSetTypePLC : BaseUCPLC
    {
        #region 定义

        #endregion 定义

        #region 初始化
        /// <summary>
        /// 构造函数
        /// </summary>
        public UCSetTypePLC()
        {
            InitializeComponent();

            NameClass = "UCSetTypePLC";

            //初始化语言
            InitLanguage();
        }

        void InitLanguage()
        {
            try
            {
                if (ParLanguage.P_I.Language_e == Language_enum.EN)
                {
                    gpbPar.Header = "Par";

                    lblModel.Content = "PLCModel";
                    lblStation.Content = "Station";
                    lblProtocol.Content = "Protocol";
                    lblMonitor.Content = "Monitor Reg";
                    lblTimeInterval.Content = "Interval";
                    lblSingleTask.Content = "SingleTask";
                    lblPreClear.Content = "PreClearReg";
                    //lblAnotherPLCLog.Content = "AnotherPLCLog";

                    lblIP.Content = "IPAddress";
                    lblPort.Content = "Port";
                    lblChannel1.Content = "Port1";
                    lblChannel2.Content = "Port2";
                    lblChannel3.Content = "Port3";
                    lblChannel4.Content = "Port4";
                    lblChannel5.Content = "Port5";
                    lblChannel6.Content = "Port6";

                    btnSave.Content = StrSave;
                }
                else if (ParLanguage.P_I.Language_e == Language_enum.FT)
                {
                    gpbPar.Header = "基本參數";

                    lblModel.Content = "PLC型號:";
                    lblStation.Content = "PLC站號:";
                    lblProtocol.Content = "協議(64位爲FullNew):";
                    lblMonitor.Content = "是否循環監控寄存器:";
                    lblTimeInterval.Content = "循環週期:";
                    lblSingleTask.Content = "圖像處理單線程監控:";
                    lblPreClear.Content = "軟件啓動預清空觸發信號:";
                    //lblAnotherPLCLog.Content = "AnotherPLCLog";

                    lblIP.Content = "PLCIP地址:";
                    lblPort.Content = "通用端口號:";
                    lblChannel1.Content = "1通道寫端口號:";
                    lblChannel2.Content = "2通道寫端口號:";
                    lblChannel3.Content = "3通道寫端口號:";
                    lblChannel4.Content = "4通道寫端口號:";
                    lblChannel5.Content = "5通道寫端口號:";
                    lblChannel6.Content = "6通道寫端口號:";

                    btnSave.Content = StrSave;
                }
                else
                {
                    gpbPar.Header = "基本参数";

                    lblModel.Content = "PLC型号：";
                    lblStation.Content = "PLC站号:";
                    lblProtocol.Content = "协议(64位为FullNew):";
                    lblMonitor.Content = "是否循环监控寄存器:";
                    lblTimeInterval.Content = "循环周期:";
                    lblSingleTask.Content = "图像处理单线程监控:";
                    lblPreClear.Content = "软件启动预清空触发信号:";
                    //lblAnotherPLCLog.Content = "AnotherPLCLog";

                    lblIP.Content = "PLCIP地址:";
                    lblPort.Content = "通用端口号:";
                    lblChannel1.Content = "1通道写端口号:";
                    lblChannel2.Content = "2通道写端口号:";
                    lblChannel3.Content = "3通道写端口号:";
                    lblChannel4.Content = "4通道写端口号:";
                    lblChannel5.Content = "5通道写端口号:";
                    lblChannel6.Content = "6通道写端口号:";

                    btnSave.Content = StrSave;
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        public override void Init()
        {
            ShowPar_Invoke();
        }
        #endregion 初始化            

        #region 参数调整
        /// <summary>
        /// 选择PLC型号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboPLCModel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (cboPLCModel.IsMouseOver)
                {
                    ParSetPLC.P_I.TypePLC_e = (TypePLC_enum)this.cboPLCModel.SelectedIndex;
                    ShowIPPort();
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        /// <summary>
        /// PLC独立写通道
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbSingleWrite_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tsbSingleWrite.IsMouseOver)
                {
                    gdIPPort.Visibility = Visibility.Visible;
                    lblIP.Visibility = Visibility.Hidden;
                    txtIP.Visibility = Visibility.Hidden;
                    lblPort.Visibility = Visibility.Hidden;
                    dudPort.Visibility = Visibility.Hidden;

                    ParSetPLC.P_I.BlSingleWrite = true;
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        private void tsbSingleWrite_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tsbSingleWrite.IsMouseOver)   
                {
                    gdIPPort.Visibility = Visibility.Hidden;
                    ParSetPLC.P_I.BlSingleWrite = false;
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }
        #endregion 参数调整

        #region 保存
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string info = StrSave;
            try
            {
                //备份文件到本地
                FunBackup.F_I.BackupPLC();

                ParSetPLC.P_I.NoStation = (int)dudNoStation.Value;
                ParSetPLC.P_I.TypePLC_e = (TypePLC_enum)this.cboPLCModel.SelectedIndex;
                ParSetPLC.P_I.TypePLCProtocol_e = (TypePLCProtocol_enum)this.cboPLCProtocol.SelectedIndex;

                ParSetPLC.P_I.BlReadCycReg = (bool)tsbReadCyc.IsChecked;

                ParSetPLC.P_I.BlRSingleTaskCamera = (bool)tsbSingleTaskCamera.IsChecked;//相机触发单线程
                ParSetPLC.P_I.Delay = (int)dudDelay.Value;//读取延迟

                ParSetPLC.P_I.BlClearAllTrigger = (bool)tsbClearAllTrigger.IsChecked;//清空触发信号

                ParSetPLC.P_I.BlAnnotherPLC = (bool)tsbAnnotherPLC.IsChecked;//PLC独立程序


                //ParSetPLC.P_I.BlAutoConnect = (bool)tsbAutoConnect.IsChecked;//PLC掉线自动重连

                //IPPort
                ParSetPLC.P_I.IP = txtIP.Text;
                ParSetPLC.P_I.Port = (int)dudPort.Value;

                ParSetPLC.P_I.BlWritePort1 = (bool)tsbWriteChannel1.IsChecked;
                ParSetPLC.P_I.BlWritePort2 = (bool)tsbWriteChannel2.IsChecked;
                ParSetPLC.P_I.BlWritePort3 = (bool)tsbWriteChannel3.IsChecked;
                ParSetPLC.P_I.BlWritePort4 = (bool)tsbWriteChannel4.IsChecked;
                ParSetPLC.P_I.BlWritePort5 = (bool)tsbWriteChannel5.IsChecked;
                ParSetPLC.P_I.BlWritePort6 = (bool)tsbWriteChannel6.IsChecked;

                ParSetPLC.P_I.PortWrite1 = (int)dudPortWrite1.Value;
                ParSetPLC.P_I.PortWrite2 = (int)dudPortWrite2.Value;
                ParSetPLC.P_I.PortWrite3 = (int)dudPortWrite3.Value;
                ParSetPLC.P_I.PortWrite4 = (int)dudPortWrite4.Value;
                ParSetPLC.P_I.PortWrite5 = (int)dudPortWrite5.Value;
                ParSetPLC.P_I.PortWrite6 = (int)dudPortWrite6.Value;

                if (ParSetPLC.P_I.WrtiteIniTypePLC())
                {
                    this.btnSave.RefreshDefaultColor(OKSave, true);
                }
                else
                {
                    this.btnSave.RefreshDefaultColor(NGSave, false);
                    info = "保存失败";
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
                this.btnSave.RefreshDefaultColor("保存失败", false);
                info = "保存失败";
            }
            finally
            {
                //按钮日志
                FunLogButton.P_I.AddInfo("btnSave保存", "设置PLC类型" + info);
            }
        }
        #endregion 保存

        #region 显示
        public override void ShowPar()
        {
            try
            {
                this.cboPLCModel.SelectedIndex = (int)ParSetPLC.P_I.TypePLC_e;
                dudNoStation.Value = ParSetPLC.P_I.NoStation;//逻辑站号

                tsbReadCyc.IsChecked = ParSetPLC.P_I.BlReadCycReg;
                this.cboPLCProtocol.SelectedIndex = (int)ParSetPLC.P_I.TypePLCProtocol_e;
                tsbReadCyc.IsChecked = ParSetPLC.P_I.BlReadCycReg;
                tsbSingleTaskCamera.IsChecked = ParSetPLC.P_I.BlRSingleTaskCamera;
                dudDelay.Value = ParSetPLC.P_I.Delay;

                //PLC独立通信
                tsbAnnotherPLC.IsChecked = ParSetPLC.P_I.BlAnnotherPLC;
                //tsbAutoConnect.IsChecked = ParSetPLC.P_I.BlAutoConnect;

                //IP Port
                txtIP.Text = ParSetPLC.P_I.IP;
                dudPort.Value = ParSetPLC.P_I.Port;

                //写入端口
                tsbWriteChannel1.IsChecked = ParSetPLC.P_I.BlWritePort1;
                tsbWriteChannel2.IsChecked = ParSetPLC.P_I.BlWritePort2;
                tsbWriteChannel3.IsChecked = ParSetPLC.P_I.BlWritePort3;
                tsbWriteChannel4.IsChecked = ParSetPLC.P_I.BlWritePort4;
                tsbWriteChannel5.IsChecked = ParSetPLC.P_I.BlWritePort5;
                tsbWriteChannel6.IsChecked = ParSetPLC.P_I.BlWritePort6;

                dudPortWrite1.Value = ParSetPLC.P_I.PortWrite1;
                dudPortWrite2.Value = ParSetPLC.P_I.PortWrite2;
                dudPortWrite3.Value = ParSetPLC.P_I.PortWrite3;
                dudPortWrite4.Value = ParSetPLC.P_I.PortWrite4;
                dudPortWrite5.Value = ParSetPLC.P_I.PortWrite5;
                dudPortWrite6.Value = ParSetPLC.P_I.PortWrite6;

                //控件是否显示
                ShowIPPort();

                //清空触发信号
                tsbClearAllTrigger.IsChecked = ParSetPLC.P_I.BlClearAllTrigger;

                //独立写通道
                tsbSingleWrite.IsChecked= ParSetPLC.P_I.BlSingleWrite;
                
                #region 权限设置
                base.SetBtnEnable(this.gdLayout, Authority.Authority_e);
                #endregion 权限设置
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        /// <summary>
        /// 根据不同的PLC，是否显示IP配置
        /// </summary>
        void ShowIPPort()
        {
            try
            {
                if (ParSetPLC.P_I.TypePLC_e == TypePLC_enum.MIT_Hls
                    || ParSetPLC.P_I.TypePLC_e == TypePLC_enum.PAN_Hls
                    || ParSetPLC.P_I.TypePLC_e == TypePLC_enum.PAN_ModBusTCP
                    || ParSetPLC.P_I.TypePLC_e == TypePLC_enum.Keyence
                    || ParSetPLC.P_I.BlSingleWrite)
                {
                    gdIPPort.Visibility = Visibility.Visible;

                    if (ParSetPLC.P_I.TypePLC_e == TypePLC_enum.MIT)
                    {
                        lblIP.Visibility = Visibility.Hidden;
                        txtIP.Visibility = Visibility.Hidden;
                        lblPort.Visibility = Visibility.Hidden;
                        dudPort.Visibility = Visibility.Hidden;
                    }
                }
                else
                {
                    gdIPPort.Visibility = Visibility.Hidden;
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }

        #endregion 显示

        
    }
}
